//
//  YYMineVC.m
//  Project
//
//  Created by Yown on 2024/1/15.
//

#import "YYMineVC.h"
#import "YYMineView.h"
#import "YYMineCellModel.h"

#import <BIToolModule/BIUserDefault.h>
#import <BIToolModule/NSString+Device.h>
#import <BIProjectModule/BIAppDefine.h>
#import <BIToolModule/NSArray+Helper.h>
#import <BIToolModule/NSDictionary+Helper.h>

#import <Masonry/Masonry.h>

@interface YYMineVC ()

@property (nonatomic) YYMineView *mineView;
@property (nonatomic) NSMutableArray <YYMineCellModel *>*cellArray;

@end

@implementation YYMineVC

#pragma mark - Life Cycle

+ (void)load {
    [[BIRouteManager sharedInstance] registerCMD:@"mine" objectOrClass:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupDatas];
    [self setupViews];
    [self setupLayouts];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    YYMineCellModel *model = [self.cellArray safeObjectAtIndex:1];
    model.desc = [self getLanguageName];
    [self.mineView reloadView:self.cellArray];
}

- (void)setupDatas {
#warning todo 用户指南的URL
    
    NSArray *titleArray = @[@"about_title", @"language_title", @"user_guide_title", @"user_feedback_title"];
    NSArray *iconArray = @[@"mine_about", @"mine_language", @"mine_userguide", @"mine_feedback"];
    NSArray *descArray = @[[NSString appVersion], [self getLanguageName], @"", @""];
    NSArray *cmdArray = @[@"about_app", @"language", @"", @"user_feedback"];
    NSArray *checkArray = @[@(NO), @(NO), @(NO), @(NO)];

    self.cellArray = [NSMutableArray array];
    for (NSUInteger i = 0; i < titleArray.count; i++) {
        YYMineCellModel *model = [YYMineCellModel new];
        model.title = [titleArray safeObjectAtIndex:i];
        model.iconName = [iconArray safeObjectAtIndex:i];
        model.desc = [descArray safeObjectAtIndex:i];
        model.arrowName = @"mine_arrow";
        model.jumpCmd = [cmdArray safeObjectAtIndex:i];
        model.isCheckLogin = [[checkArray safeObjectAtIndex:i] boolValue];
        [self.cellArray addObject:model];
    }
}

- (void)setupViews {
    self.yy_navigationItem.title = BILocalizedString(@"tab_mine");
    self.yy_navigationBar.naviBgColor = BIColor(BIAppMainBGColor);
    YYNavigationBarButton *settingButton = [YYNavigationBarButton buttonWithImage:[UIImage imageNamed:@"mine_setting"] handler:^(UIButton *sender) {
        [[BIRouteManager sharedInstance] openVCCMD:@"setting" isPresent:NO arguments:nil];
    }];
    self.yy_navigationItem.rightButtons = @[settingButton];

    self.mineView = [YYMineView view];
    [self.view addSubview:self.mineView];
}

- (void)setupLayouts {
    [self.mineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(BINavigationBarHeight);
    }];
}

#pragma mark - Private Method

- (NSString *)getLanguageName {
    NSUInteger index = [[[BIUserDefault sharedInstance] objectForKey:@"YYLanguageSettingIndex"] unsignedIntegerValue];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"language" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSDictionary *map = [array safeObjectAtIndex:index];
    return [map safeStringForKey:@"name"];
}

#pragma mark - Replace Super Method

- (void)languageChanged:(NSNotification *)notification {
    self.yy_navigationItem.title = BILocalizedString(@"tab_mine");
}

@end
